package Model;

import java.util.ArrayList;
import java.util.HashMap;

import parser.RequestParser;
import prjClasses.User;

public class Authorization {
	private final String user = "user";

	public boolean authorization(String login, String pass) {
		boolean result = false;
		User myUser = authorizationGetUser(login, pass);
		if (myUser != null)
			result = true;
		return result;
	}

	// Returns null if authorization failed
	public User authorizationGetUser(String login, String pass) {
		ArrayList<Object> users = getUserByLogPass(login, pass);
		Object result = null;
		if (users.size() == 1)
			result = users.get(0);
		return (User) result;
	}

	private ArrayList<Object> getUserByLogPass(String login, String pass) {
		RequesToService service = new RequesToService();
		HashMap<String, ArrayList<Object>> allObj = getObjectMap(service
				.getRequestAuthorization(login, pass));
		ArrayList<Object> users = allObj.get(user);
		if (users == null)
			users = new ArrayList<Object>();
		if (users.size() > 1)
			System.out.println("[X] Login not unique!");
		return users;
	}

	private HashMap<String, ArrayList<Object>> getObjectMap(String[][] data) {
		RequestParser par = new RequestParser();
		return par.parseRequest(data);
	}

}
